/**
 * Created by ZT on 2018/7/5 0005.
 */
import Vue from 'vue'
import Router from 'vue-router'
import axios  from 'axios'
import commonFunc from '../../static/js/public'
(function (window) {
    const service =axios.create({
        baseURL:'http://127.0.0.1:8081/back',
        timeout:1000*60,

    });
    service.interceptors.request.use(function (config) {
        config.headers['access_token'] = commonFunc.getCookie('access_token');
        return config;
    }, function (error){
        console.error('请求异常',error);
        return Promise.reject(error);
    });

    // 添加响应拦截器
    service.interceptors.response.use(function (response) {
        if(response.data.ret==0){
            console.log(response.data.code=='401',567);
            if(response.data.code=='401'){
               window.location.href='/backend.html';
            }
            if(Vue){
                let app = new Vue({template:'<div></div>'});
                app.$Message.error(response.data.msg)
            }
        }else{
            return response;
        }
    }, function (error) {
        return Promise.reject(error);
    });

    let fetch = service;
    window.fetch=fetch;
    if(Vue){
        Vue.prototype.fetch = fetch;
    }
})(window);